/*******************************************************************************
 * @(#)MyDialog.java 2016年8月31日
 *
 * Copyright 2016 emrubik Group Ltd. All rights reserved.
 * EMRubik PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *******************************************************************************/
package com.inossem.utils.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.inossem.utils.R;

/**
 * TODO 这里请补充该类型的简述说明
 *
 * @author <a href="mailto:leij@emrubik.com">lei jia</a>
 * @version $Revision 1.0 $ 2016年8月31日 下午4:11:38
 */
public class BaseDialog extends Dialog {

    private static Context mContext;

    private static Integer mLayoutResourceId;

    private static DialogListener mDialogListener;

    protected BaseDialog(Context context) {
        this(context, R.style.BaseDialog);
    }

    protected BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static BaseDialog createDialog(Context context, Integer layoutResourceId,
                                          DialogListener dialogListener) {
        return createDialog(context, layoutResourceId, null, null, dialogListener);
    }

    public static BaseDialog createDialog(Context context, Integer layoutResourceId, Integer width,
                                          Integer height, DialogListener dialogListener) {
        mContext = context;
        mLayoutResourceId = layoutResourceId;
        mDialogListener = dialogListener;
        BaseDialog dialog = new BaseDialog(mContext);
        dialog = setDialogParam(dialog, mContext, width, height);
        return dialog;
    }

    private static BaseDialog setDialogParam(BaseDialog dialog, Context context, Integer width,
                                             Integer height) {
        int dialogWidht, dialogHeight;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        dialogWidht = (width == null ? (int) (point.x * 0.8) : width);
        dialogHeight = (height == null ? (int) (point.y * 0.4) : height);
//        if (width == null && height == null) {
//            WindowManager windowManager = (WindowManager) context
//                    .getSystemService(Context.WINDOW_SERVICE);
//            Display display = windowManager.getDefaultDisplay();
//            Point point = new Point();
//            display.getSize(point);
//
//            dialogWidht = (int) (point.x * 0.8);
//            dialogHeight = (int) (point.y * 0.4);
//        } else {
//            dialogWidht = width;
//            dialogHeight = height;
//        }
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = dialogWidht;
        lp.height = dialogHeight;
        window.setAttributes(lp);
        return dialog;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(mLayoutResourceId, null);
        RelativeLayout relativeLayout = new RelativeLayout(mContext);
        relativeLayout.setBackgroundResource(R.drawable.style_dialog_round);
        GradientDrawable myGrad = (GradientDrawable) relativeLayout.getBackground();
        Drawable drawable = view.getBackground();
        if (drawable instanceof ColorDrawable) {
            ColorDrawable colorDrawable = (ColorDrawable) drawable;
            myGrad.setColor(colorDrawable.getColor());
        } else {
            myGrad.setColor(Color.WHITE);
        }
        int paddingInfo = (int) mContext.getResources().getDimension(R.dimen.dialog_padding);
        relativeLayout.setPadding(paddingInfo, paddingInfo, paddingInfo, paddingInfo);
        relativeLayout.addView(view);
        setContentView(relativeLayout);
        mDialogListener.initView(mContext, view, this);
    }

    public interface DialogListener {
        void initView(Context context, View view, Dialog dialog);
    }
}
